package com.kamistoat.meimeimall.meimeimallauthserver.web;

import com.google.j2objc.annotations.AutoreleasePool;
import com.kamistoat.meimeimall.common.To.EsTo.RegisterUpTo;
import com.kamistoat.meimeimall.common.To.memberTo.RegisterMemberTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.constant.AuthConstant;
import com.kamistoat.meimeimall.common.constant.ThirdPartyConstant;
import com.kamistoat.meimeimall.common.dubboInterface.MemberDubboInterface;
import com.kamistoat.meimeimall.common.dubboInterface.SearchDubboInterface;
import com.kamistoat.meimeimall.common.dubboInterface.ThirdPartyDubboInterface;
import com.kamistoat.meimeimall.common.exception.BizCodeEnum;
import com.kamistoat.meimeimall.common.exception.RRException;
import com.kamistoat.meimeimall.common.utils.JwtUtils;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallauthserver.feign.MemberFeignService;
import com.kamistoat.meimeimall.meimeimallauthserver.feign.SearchFeignService;
import com.kamistoat.meimeimall.meimeimallauthserver.feign.ThirdFeignService;
import com.kamistoat.meimeimall.meimeimallauthserver.service.AuthService;
import com.kamistoat.meimeimall.meimeimallauthserver.vo.UserRegisterVo;
import io.seata.core.constants.DubboConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Api(tags = "登录页操作")
@RestController
@RequestMapping("web")
public class LoginPageController {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Reference
    MemberDubboInterface memberDubboInterface;

    /**
     * 账号密码登录
     *
     * @param loginAccount 账号
     * @param password     密码
     * @return R对象。
     */
    @ApiOperation("账号密码登录")
    @GetMapping("/login")
    @DubboAndHTTPExceptionCatch
    public R login(@RequestParam String loginAccount, @RequestParam String password) {
        String IP = "0.0.0.0";
        String IPAddr = "未实现IP定位功能";  // IP定位
        // Dubbo调用: 登录 / 存登录记录 / 登录令牌入缓存 / 返回JwtString
        // 无论Dubbo结果是否成功，直接返回即可。成功登录R中含有JwtString，失败则是异常信息
        return memberDubboInterface.login(loginAccount, password, IP, IPAddr);
    }

    /**
     * 登出。删除掉后端缓存中的登录令牌即可
     */
    @ApiOperation("登出")
    @GetMapping("/loginout")
    @DubboAndHTTPExceptionCatch
    public R loginout() {
        return memberDubboInterface.loginout();
    }
}
